<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="由于上一篇博客中写到openid已经获取到 下面我们需要做的是微信公众号的支付 首先要做的事情是还是去微信公众号去配置 登录微信公众号之后点击左侧列表—微信支付—-将商户号记下 —-登陆微信商户平台去将商户的key拿到—–点击微信支付中的开发设置 （这个授权目录的地址不是服务的地址而是你支付页面的地址的上一级） 例：   比如我的支付页面地址是http://域名 /MountainTourism/">
<meta name="keywords" content="天下事有难易乎？为之，则难者亦易矣；不为，则易者亦难矣。">
<meta property="og:type" content="article">
<meta property="og:title" content="微信支付">
<meta property="og:url" content="http://yoursite.com/2018/06/10/微信支付/index.html">
<meta property="og:site_name" content="隐">
<meta property="og:description" content="由于上一篇博客中写到openid已经获取到 下面我们需要做的是微信公众号的支付 首先要做的事情是还是去微信公众号去配置 登录微信公众号之后点击左侧列表—微信支付—-将商户号记下 —-登陆微信商户平台去将商户的key拿到—–点击微信支付中的开发设置 （这个授权目录的地址不是服务的地址而是你支付页面的地址的上一级） 例：   比如我的支付页面地址是http://域名 /MountainTourism/">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-06-10T11:37:58.444Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="微信支付">
<meta name="twitter:description" content="由于上一篇博客中写到openid已经获取到 下面我们需要做的是微信公众号的支付 首先要做的事情是还是去微信公众号去配置 登录微信公众号之后点击左侧列表—微信支付—-将商户号记下 —-登陆微信商户平台去将商户的key拿到—–点击微信支付中的开发设置 （这个授权目录的地址不是服务的地址而是你支付页面的地址的上一级） 例：   比如我的支付页面地址是http://域名 /MountainTourism/">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/06/10/微信支付/"/>





  <title>微信支付 | 隐</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">隐</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/06/10/微信支付/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Wu qiwei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="隐">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">微信支付</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-10T19:37:38+08:00">
                2018-06-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>由于上一篇博客中写到openid已经获取到 下面我们需要做的是微信公众号的支付</p>
<p>首先要做的事情是还是去微信公众号去配置</p>
<p>登录微信公众号之后点击左侧列表—微信支付—-将商户号记下 —-登陆微信商户平台去将商户的key拿到—–点击微信支付中的开发设置</p>
<p>（这个授权目录的地址不是服务的地址而是你支付页面的地址的上一级）</p>
<pre><code>例：
  比如我的支付页面地址是http://域名 /MountainTourism/tshd/tshd.html
</code></pre><p>那么授权目录这块就写http://域名/MountainTourism/tshd/</p>
<p>做之前最好看一下微信支付的API</p>
<p><a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.PHP?chapter=7_7&amp;index=6" target="_blank" rel="noopener">https://pay.weixin.qq.com/wiki/doc/api/jsapi.PHP?chapter=7_7&amp;index=6</a></p>
<p>里面有参数的详解</p>
<p>下面直接贴代码 只贴核心的代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">/** </span><br><span class="line">     * 微信支付 </span><br><span class="line">     * @author:wuqiwei </span><br><span class="line">     * @param @param request </span><br><span class="line">     * @param @param response </span><br><span class="line">     * @param @return </span><br><span class="line">     * @param @throws ServletException </span><br><span class="line">     * @param @throws IOException </span><br><span class="line">     * @date:2017年4月5日上午10:45:42 </span><br><span class="line">     */  </span><br><span class="line">    @RequestMapping(value = &quot;/WeChatPay&quot;,produces=&quot;text/html;charset=UTF-8;&quot;, method = RequestMethod.GET)  </span><br><span class="line">    public String WeChatPay(final HttpServletRequest request,final HttpServletResponse response) throws ServletException, IOException &#123;  </span><br><span class="line">        //网页授权后获取传递的参数  </span><br><span class="line">        String money = request.getParameter(&quot;money&quot;);//分为单位  </span><br><span class="line">        //金额转化为分为单位  </span><br><span class="line">//        float sessionmoney = Float.parseFloat(money);  </span><br><span class="line">//        String finalmoney = String.format(&quot;%.2f&quot;, sessionmoney);  </span><br><span class="line">//        finalmoney = finalmoney.replace(&quot;.&quot;, &quot;&quot;);  </span><br><span class="line">//        System.out.println(&quot;money&quot;+finalmoney);  </span><br><span class="line">        String openid = request.getParameter(&quot;openId&quot;);  </span><br><span class="line">//      System.out.println(&quot;openid&quot;+openid);  </span><br><span class="line">          String outTradeNum = System.currentTimeMillis() + &quot;&quot;; // 商户订单号  </span><br><span class="line">          Map&lt;String, String&gt; wxPayParamMap = null;  </span><br><span class="line">          try &#123;  </span><br><span class="line">              wxPayParamMap = WXJSPay.jsApiPay(openid, money, outTradeNum,UUID.randomUUID().toString()); // 测试金额为1分钱  </span><br><span class="line">          &#125; catch (JDOMException e) &#123;  </span><br><span class="line">              e.printStackTrace();  </span><br><span class="line">          &#125;  </span><br><span class="line">          JSONObject json = JSONObject.fromObject(wxPayParamMap);   </span><br><span class="line">          String result = json.toString();  </span><br><span class="line">          System.out.println(result);  </span><br><span class="line">          return result;  </span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line">package com.htht.tourism.utils;  </span><br><span class="line">  </span><br><span class="line">import java.io.IOException;  </span><br><span class="line">import java.util.Map;  </span><br><span class="line">import java.util.SortedMap;  </span><br><span class="line">import java.util.TreeMap;  </span><br><span class="line">  </span><br><span class="line">import org.jdom.JDOMException;  </span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">/** </span><br><span class="line"> *  </span><br><span class="line"> * 调起微信jsapi </span><br><span class="line"> */  </span><br><span class="line">public final class WXJSPay &#123;  </span><br><span class="line">    private WXJSPay() &#123;  </span><br><span class="line">        throw new Error(&quot;工具类不能实例化！&quot;);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 调起微信jsapi </span><br><span class="line">     */  </span><br><span class="line">    @SuppressWarnings(&quot;rawtypes&quot;)  </span><br><span class="line">    public static Map jsApiPay(final String openid, final String totalFee, final String outTradeNum,  </span><br><span class="line">          final String callbackid) throws JDOMException, IOException &#123;  </span><br><span class="line">        String noceStr = Sha1Util.getNonceStr(); // 随机字符串  </span><br><span class="line">        String timeStamp = Sha1Util.getTimeStamp(); // 时间戳  </span><br><span class="line">        // 接口package部分-内容----------------------------------------------------------  </span><br><span class="line">        TreeMap&lt;String, String&gt; contentMap = new TreeMap&lt;String, String&gt;();  </span><br><span class="line">        contentMap.put(&quot;appid&quot;, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;appid&quot;)); // 公众账号 ID  </span><br><span class="line">        contentMap.put(&quot;mch_id&quot;,ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;mch_id&quot;)); // 商户号  </span><br><span class="line">        contentMap.put(&quot;nonce_str&quot;, noceStr); // 随机字符串  </span><br><span class="line">        contentMap.put(&quot;body&quot;, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;body&quot;)); // 商品描述  </span><br><span class="line">        contentMap.put(&quot;out_trade_no&quot;, outTradeNum); // 商户订单号  </span><br><span class="line">        contentMap.put(&quot;total_fee&quot;, totalFee); // 订单总金额  </span><br><span class="line">        contentMap.put(&quot;spbill_create_ip&quot;, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;spbill_create_ip&quot;)); // 订单生成的机器IP  </span><br><span class="line">        contentMap.put(&quot;notify_url&quot;,ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;notify_url&quot;)); // 通知地址  </span><br><span class="line">                                                                                   // 不同类型的订单回调地址不一样，根据type来确定  </span><br><span class="line">        contentMap.put(&quot;trade_type&quot;, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;trade_type_js&quot;)); // 交易类型  </span><br><span class="line">        contentMap.put(&quot;attach&quot;, callbackid); // 订单数据预存的id  </span><br><span class="line">        contentMap.put(&quot;openid&quot;, openid); // 用户标识  </span><br><span class="line">        String wxpackage = WeiXinSignAndPackage.createPackage(&quot;UTF-8&quot;,contentMap);  </span><br><span class="line">        contentMap.put(&quot;sign&quot;, wxpackage);  </span><br><span class="line">        String result = WeiXinSignAndPackage.getPrepayId(contentMap); // 调用统一接口返回的值  </span><br><span class="line">        System.err.println(result);  </span><br><span class="line">        Map&lt;String, String&gt; map = XMLUtil.doXMLParse(result); // 调用统一接口返回的值转换为XML格式  </span><br><span class="line">        System.out.println(map);  </span><br><span class="line">        System.out.println(&quot;---------------------&quot;);  </span><br><span class="line">        SortedMap&lt;Object, Object&gt; wxPayParamMap = new TreeMap&lt;Object, Object&gt;();  </span><br><span class="line">        wxPayParamMap.put(&quot;appId&quot;,ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;appid&quot;));  </span><br><span class="line">        wxPayParamMap.put(&quot;timeStamp&quot;, timeStamp);  </span><br><span class="line">        wxPayParamMap.put(&quot;nonceStr&quot;, noceStr);  </span><br><span class="line">        wxPayParamMap.put(&quot;package&quot;, &quot;prepay_id=&quot; + map.get(&quot;prepay_id&quot;));  </span><br><span class="line">        wxPayParamMap.put(&quot;signType&quot;,ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;signType&quot;));  </span><br><span class="line">        String paySign = WeiXinSignAndPackage.createPaySign(&quot;UTF-8&quot;,wxPayParamMap); // 支付得到的签名  </span><br><span class="line">        wxPayParamMap.put(&quot;paySign&quot;, paySign);  </span><br><span class="line">        wxPayParamMap.put(&quot;payMoney&quot;, totalFee); // 到前段显示使用，支付不需要此参数  </span><br><span class="line">        return wxPayParamMap;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 测试主函数 </span><br><span class="line">     */  </span><br><span class="line">    public static void main(final String[] args) &#123;  </span><br><span class="line">        String outTradeNum = System.currentTimeMillis() + &quot;&quot;; // 商户订单号  </span><br><span class="line">        System.out.println(outTradeNum);  </span><br><span class="line">        try &#123;  </span><br><span class="line">            jsApiPay(&quot;wx561844b33b2fd788&quot;, &quot;1000&quot;, &quot;12313&quot;, &quot;id&quot;);  </span><br><span class="line">        &#125; catch (JDOMException e) &#123;  </span><br><span class="line">            e.printStackTrace();  </span><br><span class="line">        &#125; catch (IOException e) &#123;  </span><br><span class="line">            e.printStackTrace();  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>WeiXinSignAndPackage.java(创建支付包Package、创建支付签名paysign)<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br></pre></td><td class="code"><pre><span class="line">package com.htht.tourism.utils;  </span><br><span class="line">  </span><br><span class="line">import java.net.URLEncoder;  </span><br><span class="line">import java.text.SimpleDateFormat;  </span><br><span class="line">import java.util.Date;  </span><br><span class="line">import java.util.Iterator;  </span><br><span class="line">import java.util.Map;  </span><br><span class="line">import java.util.Map.Entry;  </span><br><span class="line">import java.util.Set;  </span><br><span class="line">import java.util.SortedMap;  </span><br><span class="line">import java.util.TreeMap;  </span><br><span class="line">  </span><br><span class="line">/** </span><br><span class="line"> *  </span><br><span class="line"> * 创建支付包Package </span><br><span class="line"> * </span><br><span class="line"> */  </span><br><span class="line">public final class WeiXinSignAndPackage &#123;  </span><br><span class="line">    private WeiXinSignAndPackage() &#123;  </span><br><span class="line">        throw new Error(&quot;工具类不能实例化！&quot;);  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 创建支付包Package </span><br><span class="line">     *  </span><br><span class="line">     * @param treeMap </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    public static String createPackage(String characterEncoding, SortedMap&lt;String, String&gt; parameters) &#123;  </span><br><span class="line">//        String string1 = originalString(treeMap);  </span><br><span class="line">//        String stringSignTemp = string1 + &quot;key=&quot; + ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;paternerKey&quot;);  </span><br><span class="line">//        String sign = MD5Util.md5Encode(stringSignTemp, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;input_charset&quot;)).toUpperCase();  </span><br><span class="line">//        return sign;  </span><br><span class="line">         StringBuffer sb = new StringBuffer();  </span><br><span class="line">         Set es = parameters.entrySet();  </span><br><span class="line">         Iterator it = es.iterator();  </span><br><span class="line">         while(it.hasNext()) &#123;  </span><br><span class="line">             Map.Entry entry = (Map.Entry)it.next();  </span><br><span class="line">             String k = (String)entry.getKey();  </span><br><span class="line">             Object v = entry.getValue();  </span><br><span class="line">             if(null != v &amp;&amp; !&quot;&quot;.equals(v)   </span><br><span class="line">                     &amp;&amp; !&quot;sign&quot;.equals(k) &amp;&amp; !&quot;key&quot;.equals(k)) &#123;  </span><br><span class="line">                 sb.append(k + &quot;=&quot; + v + &quot;&amp;&quot;);  </span><br><span class="line">             &#125;  </span><br><span class="line">         &#125;  </span><br><span class="line">         sb.append(&quot;key=&quot; + ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;paternerKey&quot;));  </span><br><span class="line">         String sign = MD5Util.md5Encode(sb.toString(), characterEncoding).toUpperCase();  </span><br><span class="line">         return sign;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 创建支付签名paysign </span><br><span class="line">     *  </span><br><span class="line">     * @param wxpackage </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    public static String createPaySign(String characterEncoding, SortedMap&lt;Object, Object&gt; parameters) &#123;  </span><br><span class="line">//        String string1 = originalString(param);  </span><br><span class="line">//        String stringSignTemp = string1 + &quot;key=&quot; + ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;paternerKey&quot;);  </span><br><span class="line">//        System.out.println(&quot;签名调试输出：&quot; + stringSignTemp);  </span><br><span class="line">//        String paysign = MD5Util.md5Encode(stringSignTemp, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;input_charset&quot;)).toUpperCase();  </span><br><span class="line">//        return paysign;  </span><br><span class="line">         StringBuffer sb = new StringBuffer();  </span><br><span class="line">         Set es = parameters.entrySet();  </span><br><span class="line">         Iterator it = es.iterator();  </span><br><span class="line">         while(it.hasNext()) &#123;  </span><br><span class="line">             Map.Entry entry = (Map.Entry)it.next();  </span><br><span class="line">             String k = (String)entry.getKey();  </span><br><span class="line">             Object v = entry.getValue();  </span><br><span class="line">             if(null != v &amp;&amp; !&quot;&quot;.equals(v)   </span><br><span class="line">                     &amp;&amp; !&quot;sign&quot;.equals(k) &amp;&amp; !&quot;key&quot;.equals(k)) &#123;  </span><br><span class="line">                 sb.append(k + &quot;=&quot; + v + &quot;&amp;&quot;);  </span><br><span class="line">             &#125;  </span><br><span class="line">         &#125;  </span><br><span class="line">         sb.append(&quot;key=&quot; + ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;paternerKey&quot;));  </span><br><span class="line">         String sign = MD5Util.md5Encode(sb.toString(), characterEncoding).toUpperCase();  </span><br><span class="line">         return sign;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * wxpackage组装原始串 </span><br><span class="line">     *  </span><br><span class="line">     * @param treeMap </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    private static String originalString(final TreeMap&lt;String, String&gt; treeMap) &#123;  </span><br><span class="line">        Set&lt;Entry&lt;String, String&gt;&gt; entry = treeMap.entrySet();  </span><br><span class="line">        StringBuffer sb = new StringBuffer();  </span><br><span class="line">        for (Entry&lt;String, String&gt; obj : entry) &#123;  </span><br><span class="line">            String k = obj.getKey();  </span><br><span class="line">            String v = obj.getValue();  </span><br><span class="line">            if (v == null &amp;&amp; &quot;&quot;.equals(v)) &#123;  </span><br><span class="line">                continue;  </span><br><span class="line">            &#125;  </span><br><span class="line">            sb.append(k + &quot;=&quot; + v + &quot;&amp;&quot;);  </span><br><span class="line">        &#125;  </span><br><span class="line">        return sb.toString();  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 原url </span><br><span class="line">     */  </span><br><span class="line">    @SuppressWarnings(&quot;unused&quot;)  </span><br><span class="line">    private static String originalURLString(final TreeMap&lt;String, String&gt; treeMap) &#123;  </span><br><span class="line">        Set&lt;Entry&lt;String, String&gt;&gt; entry = treeMap.entrySet();  </span><br><span class="line">        StringBuffer sb = new StringBuffer();  </span><br><span class="line">        try &#123;  </span><br><span class="line">            for (Entry&lt;String, String&gt; obj : entry) &#123;  </span><br><span class="line">                String k = obj.getKey();  </span><br><span class="line">                String v = obj.getValue();  </span><br><span class="line">                if (v == null &amp;&amp; &quot;&quot;.equals(v)) &#123;  </span><br><span class="line">                    continue;  </span><br><span class="line">                &#125;  </span><br><span class="line">                sb.append(k.toLowerCase() + &quot;=&quot; + URLEncoder.encode(v,ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;input_charset&quot;)) + &quot;&amp;&quot;);  </span><br><span class="line">            &#125;  </span><br><span class="line">        &#125; catch (Exception e) &#123;  </span><br><span class="line">        &#125;  </span><br><span class="line">        return sb.toString();  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 创建微信支付订单号 </span><br><span class="line">     *  </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    public static String getOutTradeNo() &#123;  </span><br><span class="line">        SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyyMMddhhmmss&quot;);  </span><br><span class="line">        String body = String.valueOf((int) (Math.random() * ConstantNumbers.NUMBER_100000000));  </span><br><span class="line">        String outTradeNo = &quot;WXP&quot; + sdf.format(new Date()) + body;  </span><br><span class="line">        System.out.println(&quot;创建支付订单号：&quot; + outTradeNo);  </span><br><span class="line">        return outTradeNo;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 创建格式为yyyyMMddHHmmss的当前时间串 </span><br><span class="line">     *  </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    public static String getNowTime() &#123;  </span><br><span class="line">        SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyyMMddHHmmss&quot;);  </span><br><span class="line">        return sdf.format(new Date());  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 是否财付通签名,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。 </span><br><span class="line">     */  </span><br><span class="line">    public static synchronized boolean isValidSign(final TreeMap&lt;String, String&gt; treeMap) &#123;  </span><br><span class="line">        Set&lt;Entry&lt;String, String&gt;&gt; entry = treeMap.entrySet();  </span><br><span class="line">        StringBuffer sb = new StringBuffer();  </span><br><span class="line">        String signback = null;  </span><br><span class="line">        String inputCharset = null;  </span><br><span class="line">        for (Entry&lt;String, String&gt; obj : entry) &#123;  </span><br><span class="line">            String k = obj.getKey();  </span><br><span class="line">            String v = obj.getValue();  </span><br><span class="line">            if (v == null &amp;&amp; &quot;&quot;.equals(v)) &#123;  </span><br><span class="line">                continue;  </span><br><span class="line">            &#125;  </span><br><span class="line">            if (k.equals(&quot;sign&quot;)) &#123;  </span><br><span class="line">                signback = v;  </span><br><span class="line">                continue;  </span><br><span class="line">            &#125;  </span><br><span class="line">            sb.append(k.toLowerCase() + &quot;=&quot; + v + &quot;&amp;&quot;);  </span><br><span class="line">        &#125;  </span><br><span class="line">        String string1 = sb.toString();  </span><br><span class="line">        String stringSignTemp = string1 + &quot;key=&quot; + ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;paternerKey&quot;);//商户支付密钥  </span><br><span class="line">        System.out.println(&quot;程序计算签名串：&quot; + stringSignTemp);  </span><br><span class="line">        String sign = MD5Util.md5Encode(stringSignTemp, inputCharset).toUpperCase();  </span><br><span class="line">        System.out.println(&quot;程序计算财付通签名：&quot; + sign);  </span><br><span class="line">        System.out.println(&quot;系统返回签名：&quot; + signback);  </span><br><span class="line">        if (sign.equals(signback)) &#123;  </span><br><span class="line">            System.out.println(&quot;DeBug财付通签名比对结果：TRUE&quot;);  </span><br><span class="line">            return true;  </span><br><span class="line">        &#125; else &#123;  </span><br><span class="line">            System.out.println(&quot;DeBug财付通签名比对结果：FALSE&quot;);  </span><br><span class="line">            return false;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 判断微信签名 </span><br><span class="line">     *  </span><br><span class="line">     * @param treeMap </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    public static synchronized boolean isWXSign(final TreeMap&lt;String, String&gt; treeMap) &#123;  </span><br><span class="line">        Set&lt;Entry&lt;String, String&gt;&gt; entry = treeMap.entrySet();  </span><br><span class="line">        StringBuffer sb = new StringBuffer();  </span><br><span class="line">        String appSignature = null;  </span><br><span class="line">        for (Entry&lt;String, String&gt; obj : entry) &#123;  </span><br><span class="line">            String k = obj.getKey();  </span><br><span class="line">            String v = obj.getValue();  </span><br><span class="line">            if (v == null &amp;&amp; &quot;&quot;.equals(v)) &#123;  </span><br><span class="line">                continue;  </span><br><span class="line">            &#125;  </span><br><span class="line">            if (k.equals(&quot;AppSignature&quot;)) &#123;  </span><br><span class="line">                appSignature = v;  </span><br><span class="line">                continue;  </span><br><span class="line">            &#125;  </span><br><span class="line">            if (k.equals(&quot;SignMethod&quot;)) &#123;  </span><br><span class="line">                continue;  </span><br><span class="line">            &#125;  </span><br><span class="line">            sb.append(k.toLowerCase() + &quot;=&quot; + v + &quot;&amp;&quot;);  </span><br><span class="line">        &#125;  </span><br><span class="line">        String paysign = sb.toString();  </span><br><span class="line">        paysign = paysign.substring(0, paysign.length() - 1);  </span><br><span class="line">        System.out.println(&quot;\n\n程序计算微信签名串：&quot; + paysign);  </span><br><span class="line">        paysign = Sha1Util.getSha1(paysign);  </span><br><span class="line">        System.out.println(&quot;程序计算微信签名结果：&quot; + paysign);  </span><br><span class="line">        System.out.println(&quot;微信返回签名结果：&quot; + appSignature);  </span><br><span class="line">        if (paysign.equals(appSignature)) &#123;  </span><br><span class="line">            System.out.println(&quot;DeBug微信签名比对结果：TRUE&quot;);  </span><br><span class="line">            return true;  </span><br><span class="line">        &#125; else &#123;  </span><br><span class="line">            System.out.println(&quot;DeBug微信签名比对结果：FALSE&quot;);  </span><br><span class="line">            return false;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * map转换 </span><br><span class="line">     */  </span><br><span class="line">    public static TreeMap&lt;String, String&gt; mapToTreeMap(final Map&lt;Object, Object&gt; map) &#123;  </span><br><span class="line">        TreeMap&lt;String, String&gt; treeMap = new TreeMap&lt;String, String&gt;();  </span><br><span class="line">        Set&lt;Entry&lt;Object, Object&gt;&gt; entry = map.entrySet();  </span><br><span class="line">        for (Entry&lt;Object, Object&gt; key : entry) &#123;  </span><br><span class="line">            treeMap.put(key.getKey().toString(), ((String[]) key.getValue())[0].toString());  </span><br><span class="line">        &#125;  </span><br><span class="line">        return treeMap;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     *map转换 </span><br><span class="line">     */  </span><br><span class="line">    public static TreeMap&lt;String, String&gt; strmapToTreeMap(final Map&lt;String, String&gt; map) &#123;  </span><br><span class="line">        TreeMap&lt;String, String&gt; treeMap = new TreeMap&lt;String, String&gt;();  </span><br><span class="line">        Set&lt;Entry&lt;String, String&gt;&gt; entry = map.entrySet();  </span><br><span class="line">        for (Entry&lt;String, String&gt; key : entry) &#123;  </span><br><span class="line">            treeMap.put(key.getKey().toString(), key.getValue().toString());  </span><br><span class="line">        &#125;  </span><br><span class="line">        return treeMap;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 获取PrepayId </span><br><span class="line">     */  </span><br><span class="line">    public static String getPrepayId(final TreeMap&lt;String, String&gt; payParamMap) &#123;  </span><br><span class="line">        String result = HttpTool.sendPost(ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;prepay_id_url&quot;), payParamMap, ConfigUtil.getProperty(&quot;WeChat&quot;,&quot;input_charset&quot;));  </span><br><span class="line">        return result;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h6 id="xml工具类"><a href="#xml工具类" class="headerlink" title="xml工具类"></a>xml工具类</h6><p>package com.htht.tourism.utils;<br>import java.io.ByteArrayInputStream;<br>import java.io.IOException;<br>import java.io.InputStream;<br>import java.util.HashMap;<br>import java.util.Iterator;<br>import java.util.List;<br>import java.util.Map;<br>import java.util.Set;<br>import java.util.SortedMap;<br>import java.util.TreeMap;  </p>
<p>import javax.servlet.ServletInputStream;<br>import javax.servlet.http.HttpServletRequest;<br>import org.jdom.Document;<br>import org.jdom.Element;<br>import org.jdom.JDOMException;<br>import org.jdom.input.SAXBuilder;  </p>
<p>/** </p>
<ul>
<li>xml工具类 </li>
<li><p>*/<br>public final class XMLUtil {  </p>
<p> private XMLUtil() {  </p>
<pre><code>throw new Error(&quot;工具类不能实例化！&quot;);  
</code></pre><p> }  </p>
<p> /** </p>
<ul>
<li><p>解析request流对象InputStream 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。<br>*/<br>@SuppressWarnings(“all”)<br>public static Map&lt;String, String&gt; doXMLParse(final HttpServletRequest request) throws JDOMException, IOException {<br> Map&lt;String, String&gt; m = new HashMap&lt;String, String&gt;();<br> SAXBuilder builder = new SAXBuilder();<br> ServletInputStream in = request.getInputStream();<br> Document doc = builder.build(in);<br> Element root = doc.getRootElement();<br> List<object> list = root.getChildren();<br> Iterator<object> it = list.iterator();<br> while (it.hasNext()) {  </object></object></p>
<pre><code>Element e = (Element) it.next();  
String k = e.getName();  
String v = &quot;&quot;;  
List children = e.getChildren();  
if (children.isEmpty()) {  
    v = e.getTextNormalize();  
} else {  
    v = XMLUtil.getChildrenText(children);  
}  

m.put(k, v);  
</code></pre><p> }  </p>
<p> // 关闭流<br> in.close();<br> return m;<br>}  </p>
<p>/** </p>
</li>
<li>解析String类型的xml流对象InputStream </li>
<li><p>解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。<br>*/<br>@SuppressWarnings(“all”)<br>public static Map&lt;String, String&gt; doXMLParse(final String strxml) throws JDOMException, IOException {<br> if (null == strxml || “”.equals(strxml)) {  </p>
<pre><code>return null;  
</code></pre><p> }  </p>
<p> Map&lt;String, String&gt; m = new HashMap&lt;String, String&gt;();<br> InputStream in = new ByteArrayInputStream(strxml.getBytes(“UTF-8”));<br> SAXBuilder builder = new SAXBuilder();<br> Document doc = builder.build(in);<br> Element root = doc.getRootElement();<br> List list = root.getChildren();<br> Iterator it = list.iterator();<br> while (it.hasNext()) {  </p>
<pre><code>Element e = (Element) it.next();  
String k = e.getName();  
String v = &quot;&quot;;  
List children = e.getChildren();  
if (children.isEmpty()) {  
    v = e.getTextNormalize();  
} else {  
    v = XMLUtil.getChildrenText(children);  
}  
m.put(k, v);  
</code></pre><p> }  </p>
<p> // 关闭流<br> in.close();  </p>
<p> return m;<br>}  </p>
<p>/** </p>
</li>
<li><p>获取子结点的xml<br>*/<br>@SuppressWarnings(“all”)<br>private static String getChildrenText(final List children) {<br> StringBuffer sb = new StringBuffer();<br> if (!children.isEmpty()) {  </p>
<pre><code>Iterator it = children.iterator();  
while (it.hasNext()) {  
    Element e = (Element) it.next();  
    String name = e.getName();  
    String value = e.getTextNormalize();  
    List list = e.getChildren();  
    sb.append(&quot;&lt;&quot; + name + &quot;&gt;&quot;);  
    if (!list.isEmpty()) {  
        sb.append(XMLUtil.getChildrenText(list));  
    }  
    sb.append(value);  
    sb.append(&quot;&lt;/&quot; + name + &quot;&gt;&quot;);  
}  
</code></pre><p> }  </p>
<p> return sb.toString();<br>}  </p>
<p>/** </p>
</li>
<li>将请求参数转换为xml格式的string </li>
<li>通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了. </li>
<li><p>*/<br>@SuppressWarnings(“all”)<br>public static String getRequestXml(final SortedMap&lt;String, String&gt; parameters) {<br>StringBuffer sb = new StringBuffer();<br>sb.append(“<xml>“);<br>Set es = parameters.entrySet();<br>Iterator it = es.iterator();<br>while (it.hasNext()) {  </xml></p>
<pre><code>Map.Entry entry = (Map.Entry) it.next();  
String k = (String) entry.getKey();  
String v = (String) entry.getValue();  
if (&quot;attach&quot;.equalsIgnoreCase(k) || &quot;body&quot;.equalsIgnoreCase(k) || &quot;sign&quot;.equalsIgnoreCase(k)) {  
    sb.append(&quot;&lt;&quot; + k + &quot;&gt;&quot; + &quot;&lt;![CDATA[&quot; + v + &quot;]]&gt;&lt;/&quot; + k + &quot;&gt;&quot;);  
} else {  
    sb.append(&quot;&lt;&quot; + k + &quot;&gt;&quot; + v + &quot;&lt;/&quot; + k + &quot;&gt;&quot;);  
}  
</code></pre><p>}<br>sb.append(““);<br>return sb.toString();<br>}  </p>
<p>/** </p>
</li>
<li>main<br>*/<br>public static void main(final String[] arg) {<br> SortedMap&lt;String, String&gt; sm = new TreeMap&lt;String, String&gt;();<br> sm.put(“SUCCESS”, “OK”);<br> String ss = getRequestXml(sm);<br> System.out.println(ss);<br>}  </li>
</ul>
</li>
</ul>
<p>}  </p>
<h6 id="Sha1Util-java-‘api说明：-‘createSHA1Sign创建签名SHA1-‘getSha1-Sha1签名"><a href="#Sha1Util-java-‘api说明：-‘createSHA1Sign创建签名SHA1-‘getSha1-Sha1签名" class="headerlink" title="Sha1Util.java(‘api说明： ‘createSHA1Sign创建签名SHA1 ‘getSha1()Sha1签名)"></a>Sha1Util.java(‘api说明： ‘createSHA1Sign创建签名SHA1 ‘getSha1()Sha1签名)</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line">package com.htht.tourism.utils;  </span><br><span class="line">  </span><br><span class="line">import java.security.MessageDigest;  </span><br><span class="line">import java.security.NoSuchAlgorithmException;  </span><br><span class="line">import java.util.Arrays;  </span><br><span class="line">  </span><br><span class="line">/** </span><br><span class="line"> * 请求校验工具类 </span><br><span class="line"> *  </span><br><span class="line"> * @author 简爱微萌 </span><br><span class="line"> * @Email zyw205@gmial.com </span><br><span class="line"> *  </span><br><span class="line"> */  </span><br><span class="line">public class SignUtil &#123;  </span><br><span class="line">    // 与接口配置信息中的Token要一致  </span><br><span class="line">    private static String token = &quot;htht2xsly&quot;;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 验证签名 </span><br><span class="line">     *  </span><br><span class="line">     * @param signature </span><br><span class="line">     * @param timestamp </span><br><span class="line">     * @param nonce </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    public static boolean checkSignature(String signature, String timestamp,  </span><br><span class="line">            String nonce) &#123;  </span><br><span class="line">        String[] arr = new String[] &#123; token, timestamp, nonce &#125;;  </span><br><span class="line">        // 将token、timestamp、nonce三个参数进行字典序排序  </span><br><span class="line">        //Arrays.sort(arr);  </span><br><span class="line">        sort(arr);  </span><br><span class="line">        StringBuilder content = new StringBuilder();  </span><br><span class="line">        for (int i = 0; i &lt; arr.length; i++) &#123;  </span><br><span class="line">            content.append(arr[i]);  </span><br><span class="line">        &#125;  </span><br><span class="line">        MessageDigest md = null;  </span><br><span class="line">        String tmpStr = null;  </span><br><span class="line">  </span><br><span class="line">        try &#123;  </span><br><span class="line">            md = MessageDigest.getInstance(&quot;SHA-1&quot;);  </span><br><span class="line">            // 将三个参数字符串拼接成一个字符串进行sha1加密  </span><br><span class="line">            byte[] digest = md.digest(content.toString().getBytes());  </span><br><span class="line">            tmpStr = byteToStr(digest);  </span><br><span class="line">        &#125; catch (NoSuchAlgorithmException e) &#123;  </span><br><span class="line">            e.printStackTrace();  </span><br><span class="line">        &#125;  </span><br><span class="line">  </span><br><span class="line">        content = null;  </span><br><span class="line">        // 将sha1加密后的字符串可与signature对比，标识该请求来源于微信  </span><br><span class="line">        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 将字节数组转换为十六进制字符串 </span><br><span class="line">     *  </span><br><span class="line">     * @param byteArray </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    private static String byteToStr(byte[] byteArray) &#123;  </span><br><span class="line">        String strDigest = &quot;&quot;;  </span><br><span class="line">        for (int i = 0; i &lt; byteArray.length; i++) &#123;  </span><br><span class="line">            strDigest += byteToHexStr(byteArray[i]);  </span><br><span class="line">        &#125;  </span><br><span class="line">        return strDigest;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    /** </span><br><span class="line">     * 将字节转换为十六进制字符串 </span><br><span class="line">     *  </span><br><span class="line">     * @param mByte </span><br><span class="line">     * @return </span><br><span class="line">     */  </span><br><span class="line">    private static String byteToHexStr(byte mByte) &#123;  </span><br><span class="line">        char[] Digit = &#123; &apos;0&apos;, &apos;1&apos;, &apos;2&apos;, &apos;3&apos;, &apos;4&apos;, &apos;5&apos;, &apos;6&apos;, &apos;7&apos;, &apos;8&apos;, &apos;9&apos;, &apos;A&apos;,  </span><br><span class="line">                &apos;B&apos;, &apos;C&apos;, &apos;D&apos;, &apos;E&apos;, &apos;F&apos; &#125;;  </span><br><span class="line">        char[] tempArr = new char[2];  </span><br><span class="line">        tempArr[0] = Digit[(mByte &gt;&gt;&gt; 4) &amp; 0X0F];  </span><br><span class="line">        tempArr[1] = Digit[mByte &amp; 0X0F];  </span><br><span class="line">  </span><br><span class="line">        String s = new String(tempArr);  </span><br><span class="line">        return s;  </span><br><span class="line">    &#125;  </span><br><span class="line">  </span><br><span class="line">    public static void sort(String a[]) &#123;  </span><br><span class="line">        for (int i = 0; i &lt; a.length - 1; i++) &#123;  </span><br><span class="line">            for (int j = i + 1; j &lt; a.length; j++) &#123;  </span><br><span class="line">                if (a[j].compareTo(a[i]) &lt; 0) &#123;  </span><br><span class="line">                    String temp = a[i];  </span><br><span class="line">                    a[i] = a[j];  </span><br><span class="line">                    a[j] = temp;  </span><br><span class="line">                &#125;  </span><br><span class="line">            &#125;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h6 id="回调方法"><a href="#回调方法" class="headerlink" title="回调方法"></a>回调方法</h6><p>@throws IOException </p>
<pre><code>* @throws JDOMException 
*/  
</code></pre><p>   @RequestMapping(value = “callback”, method = RequestMethod.POST)<br>   public void callback(final HttpServletRequest request, final HttpServletResponse response) throws JDOMException {<br>       try {<br>           InputStream inStream = request.getInputStream();<br>           ByteArrayOutputStream outSteam = new ByteArrayOutputStream();<br>           byte[] buffer = new byte[1024];<br>           int len = 0;<br>           while ((len = inStream.read(buffer)) != -1) {<br>               outSteam.write(buffer, 0, len);<br>           }<br>           outSteam.close();<br>           inStream.close();<br>           String resultStr = new String(outSteam.toByteArray(), “utf-8”);<br>           Map&lt;String, String&gt; resultMap = XMLUtil.doXMLParse(resultStr);<br>           String outTradeNo = resultMap.get(“out_trade_no”);<br>           String transactionId = resultMap.get(“transaction_id”); // 微信支付订单号<br>           // String sign = resultMap.get(“sign”);<br>           String returnCode = resultMap.get(“return_code”); // 签名验证<br>           String openid = resultMap.get(“openid”); // 用户标识<br>           if (returnCode.equals(“SUCCESS”)) {<br>               SortedMap&lt;String, String&gt; sm = new TreeMap&lt;String, String&gt;();<br>               sm.put(“return_code”, “SUCCESS”); // 告诉微信服务器，我收到信息了，不要在调用回调action了,<br>               sm.put(“return_msg”, “OK”);<br>               String backString = XMLUtil.getRequestXml(sm);<br>               response.getWriter().write(backString); // 通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.<br>           }<br>       } catch (IOException e) {<br>           e.printStackTrace();<br>       }<br>   }<br>   以上代码就是Java后台的核心代码 将程序中的appid 及商户id等一些信息 换成你们自己的信息就可以</p>
<p>前端的代码就不贴了 微信官网有看一下就知道了</p>
<p>总结：由于我也是第一次做，写这篇文章是想记录一下自己的工作成果，和分享给一下也是新手的朋友们可以有一些帮助，最后希望有好的见解朋友可以留言讨论，大家一起学习进步。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/uploads/wechat.jpg" alt="Wu qiwei 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/uploads/alipay.jpg" alt="Wu qiwei 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/06/09/Java微信公众号网页授权/" rel="next" title="微信公众号网页授权">
                <i class="fa fa-chevron-left"></i> 微信公众号网页授权
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/uploads/avatar.png"
                alt="Wu qiwei" />
            
              <p class="site-author-name" itemprop="name">Wu qiwei</p>
              <p class="site-description motion-element" itemprop="description">如果人生没有目标,那跟咸鱼有什么区别。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-6"><a class="nav-link" href="#xml工具类"><span class="nav-number">1.</span> <span class="nav-text">xml工具类</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Sha1Util-java-‘api说明：-‘createSHA1Sign创建签名SHA1-‘getSha1-Sha1签名"><span class="nav-number">2.</span> <span class="nav-text">Sha1Util.java(‘api说明： ‘createSHA1Sign创建签名SHA1 ‘getSha1()Sha1签名)</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#回调方法"><span class="nav-number">3.</span> <span class="nav-text">回调方法</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=27902937&auto=1&height=66"></iframe>
    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Wu qiwei</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

   
</body>
</html>
